var pageii = null;
var grid = null;
$(function() {
	grid = lyGrid({
		id : 'paging',
		l_column : [ {
			colkey : "id",
			name : "id",
			hide:true
		}, {
			colkey: "cover",
			name: "图片",
			width: '90px',
			renderData: function (rowindex, data, rowdata, column) {
				if(data){
					return "<a target='_blank' href='"+rootPath+data+"'><img style='width:60px;' src='"+rootPath+data+"'></a>";
				}else{
					return "--"
				}
			}
		}
			, {
			colkey : "title",
			name : "标题"
		}, {
			colkey : "author",
			name : "作者"
		}, {
			colkey : "views",
			name : "阅读量",
		}, {
			colkey : "status",
			name : "上线",
			width : '90px',
			renderData : function(rowindex,data, rowdata, column) {
				var ck = "";
				if(data=="Y"){
					ck = "checked='checked'";
				}
				var html ='<label class="inline">';
				html+='<input id="id-button-borders" '+ck+' onclick="checkstate(this,\''+rowdata.id+'\')" type="checkbox" class="ace ace-switch ace-switch-6">';
				html+='<span class="lbl middle"></span></label>';
				return html;
			}
		}, {
			colkey : "createTime",
			name : "创建时间",
			renderData : function(rowindex,data, rowdata, column) {
				return new Date(data).format("yyyy-MM-dd hh:mm:ss");
			}
		},  ],
		jsonUrl : rootPath + '/policy/findByPage.shtml',
		checkbox : true,
		serNumber : true
	});
	$("#search").click("click", function() {// 绑定查询按扭
		var searchParams = $("#searchForm").serializeJson();// 初始化传参数
		grid.setOptions({
			data : searchParams
		});
	});
	$("#addPolicy").click("click", function() {
		addPolicy();
	});
	$("#editPolicy").click("click", function() {
		editPolicy();
	});
	$("#delPolicy").click("click", function() {
		delPolicy();
	});
});
function checkstate(obj,id){
	var d = 'N';
	if(obj.checked){
		d = 'Y';
	}
	var url = rootPath + '/policy/updateState.shtml';
	var s = CommnUtil.ajax(url, {
		"communityInfoFormMap.id" : id,
		"communityInfoFormMap.status" : d
	}, "json");
	if (s == "success") {
		layer.msg('操作成功');
	} else {
		layer.msg('操作失败');
	}
}
function editPolicy() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	var url = rootPath + '/policy/editUI.shtml?id=' + cbox;
	pageii = layer.open({
		title : "编辑",
		type : 1,
		area : [ "900px", "100%" ],
		content : CommnUtil.ajax(url)
	});
}
function addPolicy() {
	var url =rootPath + '/policy/addUI.shtml';
	pageii = layer.open({
		title : "新增",
		type : 1,
		area : [ "900px", "100%" ],
		content : CommnUtil.ajax(url)
	});
}
function delPolicy() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择删除项！！");
		return;
	}
	layer.confirm('是否删除？', function(index) {
		var url = rootPath + '/policy/deleteEntity.shtml';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if (s == "success") {
			layer.msg('删除成功');
			grid.loadData();
		} else {
			layer.msg('删除失败');
		}
	});
}